<template>
  <div class="container">
    <div class="app-container">
      <el-tree :expand-on-click-node="false" :data="depts" :props="defaultProps" default-expand-all>
        <template #default="{ data }">
          <el-row type="flex" style="width: 100%; height: 40px;" justify="space-between" align="middle">
            <span> {{ data.name }}</span>
            <el-col :span="4">
              <!-- 负责人 -->
              <span class="tree-manager"> {{ data.managerName }}</span>
              <!-- 下拉菜单 -->
              <el-dropdown @command="handleCommand(data.id, $event)">
                <span class="el-dropdown-link">
                  下拉菜单<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <addDept ref="addDept" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDepartment="getDepartmentList" />
  </div>
</template>
<script>
import { getDepartmentList, deleteDepartment } from '@/api/department'
import { translateListToTree } from '@/utils'
import addDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      depts: [{
        name: '传智教育',
        managerName: '管理员',
        children: [{
          name: '总裁办',
          managerName: '管理员'
        },
        {
          name: '行政部',
          managerName: '文吉星'
        },
        {
          name: '人事部',
          managerName: '周乐天',
          children: [{
            name: '财务核算部',
            managerName: '周杰伦'
          }, {
            name: '税务管理部',
            managerName: '林俊杰'
          }, {
            name: '薪资管理部',
            managerName: '许嵩'
          }]
        }
        ]
      }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentNodeId: null // 当前点击的节点的id
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartmentList()
      // console.log(res)
      this.depts = translateListToTree(res, 0)
    },
    // 下拉菜单 - 操作
    handleCommand(id, type) {
      console.log(type, id)
      if (type === 'add') {
        // 添加子部门
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        // 调用子组件身上的获取部门的详情的方法(表单渲染)
        // currentNodeId null
        // 原因: 显示子组件，内部的数据/方法，并没有构建完成
        //       父组件更新子组件的props ==> 异步的
        // 解决方案:
        // 让子组件完全构建成功，再调用子组件身上的方法
        // 晚一点调用子组件的方法...... 等它完全渲染完成...
        this.$nextTick(() => {
          // vue更新dom的过程是异步的
          // 批量的更新 - 提高更新性能
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        // 删除部门
        this.$confirm('您确认要删除该部门吗').then(async() => {
          await deleteDepartment(id)
          // 提示消息
          this.$message.success('删除部门成功')
          this.getDepartmentList()
        })
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 70px;
  display: inline-block;
  margin: 10px;
}
</style>
